//---------------------------------------------------------------------------

#ifndef CRtfEditH
#define CRtfEditH
//---------------------------------------------------------------------------
#include <SysUtils.hpp>
#include <Controls.hpp>
#include <Classes.hpp>
#include <Forms.hpp>
#include <ComCtrls.hpp>
#include <StdCtrls.hpp>
#include "CtrlStd.h"
#include "CGBox.h"
#include <WinDef.h>

#include "CRichEdit.h"     //Pour CRichEdit
#include "uGererFormatRTF.h" //Pour TGererFormatRTF
#include "uFormatsPapier.h" //Pour TFormatPapier
#include "CCourrier.h"      // Pour TContrainteTailleRichEdit

//---------------------------------------------------------------------------
class TRichEditSansAutoScroll:public CRichEdit
   {
   protected :
     virtual void    __fastcall CreateParams(Controls::TCreateParams &Params);
   public :
      __fastcall TRichEditSansAutoScroll(TComponent* AOwner):CRichEdit(AOwner){};
   };


typedef void __fastcall (__closure *CRtfEditEvenementOnChange)         (TObject *Sender, TRichEdit2 *RichEdit);
typedef void __fastcall (__closure *CRtfEditEvenementOnKeyDownUp)      (TObject *Sender, TRichEdit2 *RichEdit, WORD &Key, TShiftState Shift);
typedef void __fastcall (__closure *CRtfEditEvenementOnKeyPress)       (TObject *Sender, TRichEdit2 *RichEdit, char &Key);
typedef void __fastcall (__closure *CRtfEditEvenementOnGetHDCImp)      (TObject *Sender, HDC *hImp);

class PACKAGE_CTRLSTD CRtfEdit : public TCustomPanel
{
private:
  TWndMethod m_SveWindowProcEnTete, m_SveWindowProcPiedDePage,m_SveWindowProcCorps;
  CGroupBox *m_CGB_EnTete;
  CGroupBox *m_CGB_PiedDePage;
  TGererFormatRTF GereRTF;
  TFormatPapier FormatDuPapier;

  TRichEditSansAutoScroll *m_RE_EnTete;
  TRichEditSansAutoScroll *m_RE_Corps;
  TRichEditSansAutoScroll *m_RE_PiedDePage;
  TRichEditSansAutoScroll *m_RE_Courant;
  TRichEditSansAutoScroll *m_RE_DernierModifie;

  bool       m_ReadOnly;
  bool       m_PlainText;
  bool       m_AfficherEnTeteEtPiedDePage;
  bool       m_HideSelection;
  bool       m_WantReturns;
  bool       m_WantTabs;
  bool       m_WordWrap;
  bool       m_Modified;
  TColor     m_Color;
  TStringList *m_LinesEnTete;
  TStringList *m_LinesCorps;
  TStringList *m_LinesPiedDePage;
  NOM_FORMAT_PAPIER m_Format;
  TPopupMenu *m_PopupMenu;

  CRtfEditEvenementOnChange          m_OnChange;
  CRtfEditEvenementOnKeyDownUp       m_OnKeyDown;
  CRtfEditEvenementOnKeyPress        m_OnKeyPress;
  CRtfEditEvenementOnKeyDownUp       m_OnKeyUp;
  CRtfEditEvenementOnChange          m_OnSelectionChange;
  TRichEditResizeEvent               m_OnResizeRequest;
  CRtfEditEvenementOnGetHDCImp       m_OnGetHDCImp;


  void __fastcall SetReadOnly(bool bReadOnly);
  void __fastcall SetPlainText(bool bPlainText);
  void __fastcall SetAfficherEnTeteEtPiedDePage(bool bAfficherEnTeteEtPiedDePage);
  void __fastcall SetColor( TColor eColor);
  void __fastcall SetHideSelection(bool bHideSelection);
  void __fastcall SetWantReturns(bool bWantReturns);
  void __fastcall SetWantTabs(bool bWantTabs);
  void __fastcall SetWordWrap(bool bWordWrap);
  bool __fastcall GetModified();
  bool __fastcall GetCanUndo();
  void __fastcall SetLinesEnTete(TStringList *Liste);
  void __fastcall SetLinesCorps(TStringList *Liste);
  void __fastcall SetLinesPiedDePage(TStringList *Liste);
  void __fastcall RichEditChange(TObject *Sender);
  void __fastcall RichEditKeyDown(TObject *Sender, WORD &Key, TShiftState Shift);
  void __fastcall RichEditKeyUp(TObject *Sender, WORD &Key, TShiftState Shift);
  void __fastcall RichEditKeyPress(TObject *Sender,  char &Key);
  void __fastcall RichEditSelectionChange(TObject *Sender);
  void __fastcall RichEditOnEnter(TObject *Sender);
  void __fastcall PositionnerPanelRichEdit();
  void __fastcall RequestSize (Messages::TMessage &Message);
  void __fastcall SetMargeG(int MargeG);
  void __fastcall SetMargeD(int MargeD);
  void __fastcall SetMargeH(int MargeH);
  void __fastcall SetMargeB(int MargeB);
  void __fastcall SetPapierH(int PapierH);
  void __fastcall SetPapierL(int PapierL);
  void __fastcall SetPositionEnTete(double dPositionEnTete);
  void __fastcall SetPositionPiedDePage(double dPositionPiedDePage);
  void __fastcall GetRectRequest(TRect *ReqRequest, int iHauteurVoulueParRichEdit = 0);
  void __fastcall SetFormatPapier( enum NOM_FORMAT_PAPIER eFormat);
  void __fastcall CalculHauteurs(int *iHauteurVoulueParRichEdit, int *iHauteurImprimableParPage, int *iHauteurCorpsParPage) const;
  void __fastcall ContrainteHauteurChange(TObject *Sender);
  void __fastcall ContrainteMargeChange(TObject *Sender);
  bool __fastcall GetIsPortrait();
  void __fastcall SetIsPortrait(bool bIsPortrait);
  void __fastcall FormatPapierChange(int iLargeur, int iHauteur);
  void __fastcall AjusterHauteur(TRichEdit2 *RichEdit);
  TPopupMenu* __fastcall MonGetPopupMenu();
  void __fastcall SetPopupMenu(TPopupMenu* PopupMenu);

  TStrings* __fastcall GetLinesEnTete() const;
  TStrings* __fastcall GetLinesCorps() const;
  TStrings* __fastcall GetLinesPiedDePage() const;
  int m_MargeG; //Marge de gauche (en mm)
  int m_MargeD; //Marge de droite (en mm)
  int m_MargeH; //Marge du haut (en mm)
  int m_MargeB; //Marge du bas (en mm)
  int m_PapierH; //Hauteur de la page (en mm)
  int m_PapierL; //Largeur de la page (en mm)
  double m_dPositionEnTete;
  double m_dPositionPiedDePage;
  TContrainteTailleRichEdit *m_ContrainteTaille;

protected:
  void __fastcall WMCreate(TMessage &Msg);
  BEGIN_MESSAGE_MAP
   MESSAGE_HANDLER(WM_CREATE, TMessage, WMCreate)
  END_MESSAGE_MAP(TCustomPanel)

public:
  __property TRichEditSansAutoScroll *RE_EnTete     = {read=m_RE_EnTete    , write=m_RE_EnTete};
  __property TRichEditSansAutoScroll *RE_Corps      = {read=m_RE_Corps     , write=m_RE_Corps};
  __property TRichEditSansAutoScroll *RE_PiedDePage = {read=m_RE_PiedDePage, write=m_RE_PiedDePage};
  __property TRichEditSansAutoScroll *RE_Courant    = {read=m_RE_Courant   , write=m_RE_Courant};

   __fastcall CRtfEdit(TComponent* Owner);
   virtual __fastcall ~CRtfEdit();
   void __fastcall MyWndProcParentEnTete(Messages::TMessage &Message);
   void __fastcall MyWndProcParentPiedDePage(Messages::TMessage &Message);
   void __fastcall MyWndProcParentCorps(Messages::TMessage &Message);
   void __fastcall LoadFromString(const char szRTF[]);
   void __fastcall LoadFromFile(const AnsiString &aFileName);
   AnsiString __fastcall SaveToString();
   void __fastcall SaveToFile(const AnsiString &aFileName);
   void __fastcall Undo();
   void __fastcall ClearUndo();
   int  __fastcall GetHauteurTotaleNecessaire() const;
   int  __fastcall GetHauteurCorpsNecessaire() const;
   void __fastcall Clear();
   int  __fastcall GetNbPages(int iHauteurVoulueParRichEdit = 0,int iHauteurCorpsParPage = 0) const;
   bool __fastcall InsertImageFromFile(const AnsiString &aFileName);

__published:
	__property Align ;
	__property Alignment ;
	__property Anchors ;
	__property BiDiMode ;
	__property BorderWidth ;
	__property BorderStyle ;
	__property Constraints ;
	__property DragCursor ;
	__property DragKind ;
	__property DragMode ;
	__property Enabled ;
	__property ParentBiDiMode ;
	__property ParentColor ;
	__property ParentFont ;
	__property ParentShowHint ;
   __property TPopupMenu* PopupMenu = {read=MonGetPopupMenu, write=SetPopupMenu, default = NULL};
	__property ShowHint ;
	__property TabOrder ;
	__property TabStop ;
	__property Visible ;

	__property OnContextPopup ;
	__property OnDragDrop ;
	__property OnDragOver ;
	__property OnEndDock ;
	__property OnEndDrag ;
	__property OnEnter ;
	__property OnExit ;
	__property OnMouseDown ;
	__property OnMouseMove ;
	__property OnMouseUp ;

   __property bool        ReadOnly                   = { read=m_ReadOnly                  , write=SetReadOnly                  , default = false};
   __property bool        PlainText                  = { read=m_PlainText                 , write=SetPlainText                 , default = false};
   __property bool        AfficherEnTeteEtPiedDePage = { read=m_AfficherEnTeteEtPiedDePage, write=SetAfficherEnTeteEtPiedDePage, default = false};
   __property bool        HideSelection              = { read=m_HideSelection             , write=SetHideSelection             , default = true};
   __property bool        WantReturns                = { read=m_WantReturns               , write=SetWantReturns               , default = true};
   __property bool        WantTabs                   = { read=m_WantTabs                  , write=SetWantTabs                  , default = true};
   __property bool        WordWrap                   = { read=m_WordWrap                  , write=SetWordWrap                  , default = true};
   __property bool        Modified                   = { read=GetModified, stored = false};
   __property bool        CanUndo                    = { read=GetCanUndo,  stored = false};
   __property TColor      Color                      = { read=m_Color                     , write=SetColor                     , default=clWindow };

  __property CRtfEditEvenementOnChange    OnChange          = {read = m_OnChange         ,write = m_OnChange};
  __property CRtfEditEvenementOnKeyDownUp OnKeyDown         = {read = m_OnKeyDown        ,write = m_OnKeyDown};
  __property CRtfEditEvenementOnKeyPress  OnKeyPress        = {read = m_OnKeyPress       ,write = m_OnKeyPress};
  __property CRtfEditEvenementOnKeyDownUp OnKeyUp           = {read = m_OnKeyUp          ,write = m_OnKeyUp};
  __property CRtfEditEvenementOnChange    OnSelectionChange = {read = m_OnSelectionChange,write = m_OnSelectionChange};
  __property TRichEditResizeEvent         OnResizeRequest   = {read = m_OnResizeRequest  ,write = m_OnResizeRequest};
  __property CRtfEditEvenementOnGetHDCImp OnGetHDCImp       = {read = m_OnGetHDCImp      ,write = m_OnGetHDCImp};

  __property TContrainteTailleRichEdit *Contraintes = {read=m_ContrainteTaille, write=m_ContrainteTaille};

  __property int MargeG  = { read=m_MargeG                  , write=SetMargeG          , default = 25}; //Marge de gauche (en mm)
  __property int MargeD  = { read=m_MargeD                  , write=SetMargeD          , default = 25}; //Marge de droite (en mm)
  __property int MargeH  = { read=m_MargeH                  , write=SetMargeH          , default = 25}; //Marge du haut   (en mm)
  __property int MargeB  = { read=m_MargeB                  , write=SetMargeB          , default = 25}; //Marge du bas    (en mm)

  __property bool bIsPortrait = {read=GetIsPortrait          , write=SetIsPortrait     , default = true, stored = false}; //Orientation Portrait/Paysage
  __property int PapierH = { read=m_PapierH                  , write=SetPapierH        , default = 297}; //Hauteur de la page (en mm)
  __property int PapierL = { read=m_PapierL                  , write=SetPapierL        , default = 210}; //Largeur de la page (en mm)

  __property double dPositionEnTete     = { read=m_dPositionEnTete     , write=SetPositionEnTete    , default = 13};
  __property double dPositionPiedDePage = { read=m_dPositionPiedDePage , write=SetPositionPiedDePage, default = 13};
  __property enum   NOM_FORMAT_PAPIER FormatPapier = {read = m_Format, write = SetFormatPapier, default = ::A4};
};
//---------------------------------------------------------------------------
#endif
